﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Barry.BarbeSystem.Business;
using Barry.BarbeSystem.Core;
using Barry.BarbeSystem.Filters;
using Barry.BarbeSystem.Models;


namespace Barry.BarbeSystem.Controllers
{
    [CustomerAuthorizationFilter]
    public class BarberCategoryController : Controller
    {

        private IRepository<BarberCategory> _categoryRepository;

        public BarberCategoryController()
        {
            _categoryRepository = new Repository<BarberCategory>();
        }

        //
        // GET: /Default1/

        public ActionResult Index()
        {
            return View();
        }


        public JsonResult PageIndex(PageRequest request)
        {
            JsonResult jes = new JsonResult();


            var items = from item in _categoryRepository.GetAll()
                        where item.Enable == true
                        orderby item.Name
                        select new BarberCategory { Name = item.Name, Id = item.Id };
                    
            var itemCounts = items.Count();
            jes.Data = new GridResponseInfo() { rows = items.Skip((request.page - 1) * Utility.PageCount).Take(Utility.PageCount).ToList(), total = itemCounts };
            return jes;

        }
        //
        // GET: /Default1/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Default1/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Default1/Create

        [HttpPost]
        public ActionResult Create(BarberCategory model)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    model.Enable = true;
                    _categoryRepository.Save(model);
                    return RedirectToAction("Index");
                }

                return View(model);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Default1/Edit/5

        public ActionResult Edit(int id)
        {
            var model = _categoryRepository.Get(id);
            if (model == null)
                return HttpNotFound();
            return View(model);
        }

        //
        // POST: /Default1/Edit/5

        [HttpPost]
        public ActionResult Edit(BarberCategory model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //  model.Id = id;
                    _categoryRepository.Update(model);
                    return RedirectToAction("Index");
                }

                return View(model);
            }
            catch
            {
                return View(model);
            }
        }

     

        //
        // POST: /Default1/Delete/5

        [HttpPost]
        public JsonResult Delete(int id)
        {
            JsonResult j = new JsonResult();
            var obj = _categoryRepository.Get(id);
            if (obj != null)
            {
                obj.Enable = false;
                _categoryRepository.Update(obj);
                j.Data = new { success = true };

            }
            else
            {
                j.Data = new { success = false };
            }
            return j;
        }
    }
}
